package com.ruoyi.generator.config.template;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.generator.domain.ProjectEntity;
import com.ruoyi.generator.domain.TableEntity;
import com.ruoyi.generator.domain.TemplateEntity;
import com.ruoyi.generator.mapper.TableDao;
import com.ruoyi.generator.service.ITemplateService;
import com.ruoyi.generator.service.ProjectService;
import jakarta.annotation.Resource;

import java.util.List;

/**
 * 代码生成配置内容
 */
public class GeneratorConfig {
    private String template;

    @Resource
    private TableDao tableDao;

    @Resource
    private ITemplateService templateService;

    @Resource
    private ProjectService projectService;

    public GeneratorConfig(String template) {
        this.template = template;
    }

    public GeneratorInfo getGeneratorConfig(Long tableId) {

        TableEntity table = tableDao.selectById(tableId);

        GeneratorInfo generator = new GeneratorInfo();
        if (ObjectUtil.isEmpty(table)) {
            ProjectEntity projectEntity = projectService.getOne(new LambdaQueryWrapper<ProjectEntity>().eq(ProjectEntity::getStatus, '1'));
            generator.setProject(projectEntity);
        } else {
            ProjectEntity projectEntity = new ProjectEntity();
            projectEntity.setAuthor(table.getAuthor());
            projectEntity.setEmail(table.getEmail());
            generator.setProject(projectEntity);
            TemplateEntity templateEntity = new TemplateEntity();
            templateEntity.setProjectId(table.getProjectId());
            List<TemplateEntity> templateEntities = templateService.selectTemplateList(templateEntity);
            generator.setTemplates(templateEntities);
        }
        return generator;
    }
}
